<?php
namespace app\wiki\controller;

use app\common\logic\BaseLogic;
use app\common\logic\ContentLogic;
use gdbase\Annotations;
use gdbase\Tree;
use ReflectionClass;
use think\Controller;
use think\facade\Request;

class Index extends Controller
{
    public function index()
    {
        $version = input('v');

        $file = input('file',-1);
        $name = input('name');
        $text = input('text');

        $allowVersion = explode(',', env('API_ALLOW_VERSION'));
        if(empty($version)){
            $this->redirect(url('index',['v'=>$allowVersion[0]]));
        }
        $menus = [];
        $annotations = new Annotations();
        try{
            $files = read_dir(getcwd().'\..\application\api\\'.$version.'\\');
        }
        catch (\Exception $e){
            $this->error('找不到接口相关文件');
        }
        $menus = cache('wiki_menus_' . $version,'',null,'api');
        if (empty($menus)) {
            foreach ($files as $key => $item) {
                $classs = 'app\\api\\'.$version.'\\' . basename($item, ".php");
                $result = $annotations->getClassAnnotations($classs);
                if (!empty($result['classInfo']) && count($result['classInfo']) > 0) {
                    $ref = new ReflectionClass($classs);
                    // 取方法
                    $methods = $ref->getMethods();     //返回类中所有方法
                    $child = [];
                    foreach ($methods as $mm => $method) {
                        $methodjieguo = $annotations->getMethodAnnotations($classs,$method->getName());
                        if(empty($methodjieguo['isApi']) || count($methodjieguo['isApi']) < 1 || false == $methodjieguo['isApi'][0]){
                            continue;
                        }
                        cache($key . '_' . $method->getName(),$methodjieguo,null,'api');
                        if (!empty($methodjieguo['methodInfo']) && count($methodjieguo['methodInfo']) > 0) {
                            $explain = '';
                            if (!empty($methodjieguo['methodInfo'][0]['explain'])) {
                                $explain = $methodjieguo['methodInfo'][0]['explain'];
                            }
                            $child[] = [
                                'id' => $key . '_' . $mm,
                                'name' => url('index',['file' => $key, 'name'=>$method->getName() , 'text' => $methodjieguo['methodInfo'][0]['text'] ,'v'=>$version ]),
                                'title' => $methodjieguo['methodInfo'][0]['text'],
                                'explain' => $explain,
                            ];
                        }
                    }

                    $menus[] = [
                        'id' => $key,
                        'name' => $key,
                        'title' => $result['classInfo'][0]['text'],
                        '_child' => $child,
                    ];
                }
            }
            cache('wiki_menus_' . $version,$menus,null,'api');
        }

        if(!empty($name) && $file > -1){
            $methodjieguo = cache($file . '_' . $name,'',null,'api');
            if(empty($methodjieguo)){
                $this->error('读取错误');
            }
            $this->assign('url','/api/' . basename($files[$file], ".php") . '/' . $name);
            $this->assign('methodInfo',$methodjieguo);
        }
        else{
            $this->assign('methodInfo',false);
        }

//        cache('wiki_menus',$menus);
        $this->assign('allowVersions', $allowVersion);
        $this->assign('menus',$menus);
        $this->assign('menus_curr',0);
        $this->assign('listFields',[]);
        $this->assign('datas',[]);
        $this->assign('controllerName',$text);
        return $this->fetch();
    }
}
